package com.romotc.qqrobot.webqq.thread;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.math.RandomUtils;

import com.romotc.common.utils.RandomString;
import com.romotc.qqrobot.bean.QQLogedin;
import com.romotc.qqrobot.bean.SendMsg;
import com.romotc.qqrobot.bean.SendMsgService;
import com.sun.org.apache.bcel.internal.generic.NEW;
/**
 * 定期检查osc_send_msgs 表中数据进行发送
 * @author Administrator
 *
 */
public class SendMsgThread extends Thread {
	private String qqNum;
	public SendMsgThread(String qqNum) {
		this.qqNum = qqNum;
	}
	
	/**
	 * 扫描间隔时间
	 */
	private int scanTime = 3;
	
	@Override
	public void run() {
		QQLogedin curQq;
		int tempInt = 27820033;
		int j = 0;
		while((curQq = QQLogedin.getCurQqLogedIn(qqNum)) != null) {//将控制权交给QQLogedin
			//扫描数据表 并调用发送消息接口
			List<SendMsg> sendList = SendMsgService.qryMySendTask(curQq);
			
			//测试用
			j++;
			if(false && "1586030808".equals(qqNum) && j % 10 == 0) {
				SendMsg timeTest = new SendMsg();
				timeTest.setSendTo(-26);
				SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				timeTest.setMsgContext_data(RandomString.randomString(29) + fmt.format(new Date()) + RandomString.randomString(29));
				timeTest.setMsgId(tempInt++);
				timeTest.setSenderQQ(1586030808);
				sendList.add(timeTest);
				j -= 10;
				j -= RandomUtils.nextInt(20);
			}
			
			try {
				SendMsgService.sendMsg(sendList);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			
			//休眠一段时间再查询
			try {
				Thread.sleep(scanTime * 2000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
